package controller;

import model.DuplicateItemIdentifierException;
import model.Item;
import model.ItemCollection;

/**
 * 
 * @author Mads Nielsen
 *
 */
public class ItemController {
    
    private ItemCollection items;
    
    public ItemController() {
        items = ItemCollection.getInstance();
        
    }
    
    public static void loadData() throws DuplicateItemIdentifierException {
        
        Item item1 = new Item();
        item1.setItemNo("1");
        item1.setName("Vare 1");
        item1.setPrice(99.95);
        
        Item item2 = new Item();
        item2.setItemNo("2");
        item2.setName("Vare 2, 10% discount");
        item2.setPrice(199.95);
        item2.setDiscount(10);
        
        Item item3 = new Item();
        item3.setItemNo("3");
        item3.setName("Vare 3 (Samlepakke, 20%)");
        item3.setPrice(0);
        item3.setDiscount(20);
        
        Item item4 = new Item();
        item4.setItemNo("4");
        item4.setName("Vare 4");
        item4.setPrice(399.95);
        
        Item item5 = new Item();
        item5.setItemNo("5");
        item5.setName("Vare 5");
        item5.setPrice(499.95);
        
        item3.addSubItem(item4);
        item3.addSubItem(item5);
        
        ItemCollection items = ItemCollection.getInstance();
        items.addItem(item1);
        items.addItem(item2);
        items.addItem(item3);
        items.addItem(item4);
        items.addItem(item5);
        
    }
    
    public void addItem(String name, String description, String itemNo, double price, double discount)
            throws DuplicateItemIdentifierException {
        Item item = new Item();
        item.setName(name);
        item.setDescription(description);
        item.setItemNo(itemNo);
        item.setPrice(price);
        item.setDiscount(discount);
        items.addItem(item);
    }
    
    public Item[] getItems() {
        return items.getItems();
    }
    
    public Item getItem(String itemNo) {
        return items.getItem(itemNo);
    }
    
    public void removeFromSubItems(Item superItem, Item subItem) {
        superItem.removeFromSubItems(subItem);
        
    }
    
    public int getItemCount() {
        return items.getItems().length;
    }
    
    public void editItem(Item item, String newItemNo, String newItemName,
            String newItemDescription, Double newItemPrice, double discount) {
        item.setItemNo(newItemNo);
        item.setName(newItemName);
        item.setDescription(newItemDescription);
        item.setPrice(newItemPrice);
        item.setDiscount(discount);
        
    }
}
